package com.hoshiibuy.goodsAllocation.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.goodsAllocation.entity.GoodsSaleSubareaWarehouse;
import com.hoshiibuy.goodsAllocation.entity.param.GoodsSaleSubareaWarehouseInsertParam;
import com.hoshiibuy.goodsAllocation.entity.param.GoodsSaleSubareaWarehouseQueryParam;
import com.hoshiibuy.goodsAllocation.entity.param.GoodsSaleSubareaWarehouseUpdateParam;
import com.hoshiibuy.goodsAllocation.entity.vo.GoodsSaleSubareaWarehouseDetailVO;
import com.hoshiibuy.goodsAllocation.entity.vo.GoodsSaleSubareaWarehouseVO;
import com.hoshiibuy.goodsAllocation.service.IGoodsSaleSubareaWarehouseService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 商品销售分区表 前端控制器
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goodsSaleSubareaWarehouse")
@Api(tags = "商品销售分区接口")
public class GoodsSaleSubareaWarehouseController {
	@Autowired
	private IGoodsSaleSubareaWarehouseService goodsSaleSubareaWarehouseService;

	@ApiOperation(value = "新增商品销售分区表", notes = "")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response add(@Valid GoodsSaleSubareaWarehouseInsertParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		GoodsSaleSubareaWarehouse entity = new GoodsSaleSubareaWarehouse();
		BeanUtils.copyProperties(param, entity);
		// TODO 只接受指定参数
		return goodsSaleSubareaWarehouseService.insert(entity);
	}

	@ApiOperation(value = "修改商品销售分区表", notes = "")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response update(@Valid GoodsSaleSubareaWarehouseUpdateParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		GoodsSaleSubareaWarehouse entity = new GoodsSaleSubareaWarehouse();
		BeanUtils.copyProperties(param, entity);
		// TODO 只接受指定参数
		return goodsSaleSubareaWarehouseService.updateDataById(entity);
	}

	@ApiOperation(value = "删除商品销售分区表", notes = "")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response delete(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return goodsSaleSubareaWarehouseService.deleteDataById(id);
	}

	@ApiOperation(value = "获取商品销售分区表详情", notes = "")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<GoodsSaleSubareaWarehouseDetailVO> getDetail(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return goodsSaleSubareaWarehouseService.getDetail(id);
	}

	@ApiOperation(value = "获取商品销售分区表列表", notes = "")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<GoodsSaleSubareaWarehouseVO>> getList(@Valid GoodsSaleSubareaWarehouseQueryParam param,
			BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<GoodsSaleSubareaWarehouse> queryWrapper = param.getQueryWrapper();
		queryWrapper.select(BeanUtils.getSqlSelect(GoodsSaleSubareaWarehouseVO.class));
		return goodsSaleSubareaWarehouseService.getList(queryWrapper);
	}

	@ApiOperation(value = "分页查询商品销售分区表", notes = "")
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Response<IPage<GoodsSaleSubareaWarehouseVO>> getPage(@Valid GoodsSaleSubareaWarehouseQueryParam param,
			BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<GoodsSaleSubareaWarehouse> queryWrapper = param.getQueryWrapper();
		queryWrapper.select(BeanUtils.getSqlSelect(GoodsSaleSubareaWarehouseVO.class));
		IPage<GoodsSaleSubareaWarehouse> page = new Page<>(Long.parseLong(param.getPageNumber()),
				Long.parseLong(param.getPageSize()));

		return goodsSaleSubareaWarehouseService.getPage(page, queryWrapper);
	}
}
